
#include "stdafx.h"
#include "TcpPortTestPassiveConnectionActor.h"
#include "TcpPortTestAcceptActor.h"
#include "TcpPortTestApp.h"

TcpPortTestAcceptActorMgr::TcpPortTestAcceptActorMgr( boost::shared_ptr<WorkerBase> _pWorker, boost::shared_ptr<TcpPortTestPassiveConnectionActorMgr> _pTcpPortTestPassiveConnectionActorMgr_ )
    :AssignedSingleWorkerActorMgr(_pWorker),pTcpPortTestPassiveConnectionActorMgr__(_pTcpPortTestPassiveConnectionActorMgr_)
{

}

TcpPortTestAcceptActorMgr::~TcpPortTestAcceptActorMgr()
{
	pTcpPortTestPassiveConnectionActorMgr__.reset();
}

retT TcpPortTestAcceptActorMgr::makeActor( boost::shared_ptr<ActorBase>& _pActor )
{
    _pActor.reset(new TcpPortTestAcceptActor());
    R(ActorMgrBase::makeActor(_pActor));
	RETSUCCESS;
}

retT TcpPortTestAcceptActorMgr::start()
{
	R(justMakeActor());
	R(ActorMgrBase::start());
	RETSUCCESS;
}

boost::shared_ptr<TcpPortTestPassiveConnectionActorMgr> TcpPortTestAcceptActorMgr::getTcpPortTestPassiveConnectionActorMgr()
{
    return pTcpPortTestPassiveConnectionActorMgr__.lock();
}

TcpPortTestAcceptActor::TcpPortTestAcceptActor()
	:pProAcceptHandler_()
{

}

TcpPortTestAcceptActor::~TcpPortTestAcceptActor()
{
	pProAcceptHandler_.reset();
}

retT TcpPortTestAcceptActor::init()
{
    R(ActorBase::init());
	pProAcceptHandler_.reset(new ProAcceptHandler());
	if(boost::shared_ptr<ActorMgrBase> pActorMgr = pActorMgr_.lock())
	{
		R(pProAcceptHandler_->attachTcpConnectionActorMgr(
			boost::dynamic_pointer_cast<TcpPortTestAcceptActorMgr>(pActorMgr)->getTcpPortTestPassiveConnectionActorMgr()));
	}
	boost::shared_ptr<boost::asio::ip::tcp::endpoint> pLocalEndpoint(new boost::asio::ip::tcp::endpoint(
		boost::asio::ip::address::from_string(((TcpPortTestConfigure*)getApp()->getConfigure())->listeningIP_),
		((TcpPortTestConfigure*)getApp()->getConfigure())->listeningPort_));
    pProAcceptHandler_->setLocalEndpoint(pLocalEndpoint);
	pProAcceptHandler_->attachHandlerOwner(shared_from_this());
    RETSUCCESS;
}

retT TcpPortTestAcceptActor::onError( retT errorNo )
{
	errorNo = ActorBase::onError(errorNo);
	if(boost::shared_ptr<ActorMgrBase> pActorMgr = pActorMgr_.lock())
	{
		pActorMgr->post(boost::bind(&ActorMgrBase::checkToStopApp, pActorMgr));
	}
	return errorNo;
}

retT TcpPortTestAcceptActor::start()
{
	R(ActorBase::start());
	R(pProAcceptHandler_->startAccept());
	RETSUCCESS;
}
